<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8"/>
<link type="text/css" rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css" />

<link type="text/css" rel="stylesheet" href="/static/static/summernote/summernote.css" />
<script type="text/javascript" src="/static/js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="/static/js/jquery.validate.min.js"></script>

<script type="text/javascript" src="/static/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/static/static/summernote/summernote.js"></script>
<script type="text/javascript" src="/static/static/summernote/lang/summernote-zh-CN.js"></script>
<title>编辑框</title>
<style>
.error{color:red;}

</style>
</head>
<body>
<script type="text/javascript">
$(function(){
	$.fn.extend({
        serializeObject : function() {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function() {
                if (o[this.name]) {
                    if (!o[this.name].push) {
                        o[this.name] = [ o[this.name] ];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        }
    });
    $("#newblog").validate({
        rules : {
            title : {
                required : true
            }
        },
        messages : {
            title : {
                required : "标题不能为空",
                loginName_rule : "登录账号应为字母开头，允许数字下划线的5-20位组合",
                remote : "该用户名已被注册"
            }
        }
    });
    $("#release").on("click",function(){var flag=$("#newblog").validate().form();
	if(!flag){
		return;
	}saveNew(1)});
    $("#keep").on("click",function(){
    	var flag=$("#newblog").validate().form();
		if(!flag){
			return;
		}
    	saveNew(2);
    	});
	function saveNew(action){
		
		var content = $('#content').summernote('code');
		var newblog=$('#newblog').serializeObject();
		newblog['content']=content;
		newblog['status']=action;
		var jsonParam=JSON.stringify(newblog);
		$.ajax({
            async: false,
            type: "POST",
            url:'/jianghu/service/blog/addNewBlog',
            contentType : "application/json; charset=utf-8",
            data:jsonParam,
            success: function (data) {
            	
              },
            error: function () {
            	
            }
        })
	}
	
	var $summernote =$(".summernote").summernote({
        lang : 'zh-CN',
        //placeholder : placeholder,
        minHeight : 300,
        dialogsFade : true,// Add fade effect on dialogs
        dialogsInBody : true,
        // summernote.
        disableDragAndDrop : false,// default false You can disable drag
        // and drop
        callbacks : {
            onImageUpload : function(files) {
                var $files = $(files);
                $files.each(function() {
                    var file = this;
                    var data = new FormData();
                    data.append("file", file);
                    data.append("id",$("#id").val());
                    $.ajax({
                        data : data,
                        type : "POST",
                        url : '/jianghu/file/upload',
                        contentType : false,
                        processData : false,
                        success : function(response) {
                        	$summernote.summernote('insertImage',""+response.data);
                        },
                        error : '失败'
                    });
                });
            }
        }
    });
})
</script>
  <div class="container">
      <input id="id" name="id" type="text" th:value="${id}" style="display: none">
    <form  id="newblog" class="form-horizontal required-validate" action="#" enctype="multipart/form-data"  onsubmit="return">
    <div class="form-group">
        <label for="" class="col-md-2 control-label">标题:</label>
        <div class="col-md-8 tl th">
            <input type="text" id="title" name="title" class="form-control" value=""/>
        </div>
    </div>
        <div class="form-group">
            <label for="" class="col-md-2 control-label">来源:</label>
            <div class="col-md-4 tl th">
                <select class=" form-control" name="source" id="source">
                    <option value="1">转载</option>
                    <option value="2">原创</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label for="" class="col-md-2 control-label">概述:</label>
            <div class="col-md-8 tl th">
                <textarea  name="summary" class="form-control" rows="3"></textarea>
            </div>
        </div>
      <div class="form-group">
        <label for="" class="col-md-2 control-label" >内容</label>
        <div class="col-md-8">
            <div id="content" class="summernote" name="description" placeholder="" action=""></div>
        </div>
    </div>
    	<div class="form-group">
    		<center>
            <input id="release" class="btn btn-info" type="button" value="发布">
			<input id="keep" class="btn btn-default" type="button" value="暂存">
            </center>
        </div>
    </form>
</div>
</body>
</html>